<!--vue模板-->
<template>
  <div>
    <!--搜索-->
    <div class="toolbar">
      客户ID:<el-input
        placeholder="请输入"
        clearable
        style="width: 150px; margin-right: 10px"
        v-model="filters.id"
      />&nbsp;
      客户公司:<el-input
        placeholder="请输入"
        clearable
        style="width: 150px; margin-right: 10px"
        v-model="filters.companyName"
      />&nbsp;
      联系人:<el-input
        placeholder="请输入"
        clearable
        style="width: 150px; margin-right: 10px"
        v-model="filters.name"
      />&nbsp;
      联系方式:<el-input
        placeholder="请输入"
        clearable
        style="width: 150px; margin-right: 10px"
        v-model="filters.mobile"
      />&nbsp;
      邮箱:<el-input
        placeholder="请输入"
        clearable
        style="width: 150px; margin-right: 10px"
        v-model="filters.email"
      /><br/>
      感兴趣产品:<el-input
        placeholder="请输入"
        clearable
        style="width: 150px; margin-right: 10px"
        v-model="filters.productName"
      />&nbsp;
      国家:<el-input
        placeholder="请输入"
        clearable
        style="width: 150px; margin-right: 10px"
        v-model="filters.country"
      />&nbsp;
      市场:<el-input
        placeholder="请输入"
        clearable
        style="width: 150px; margin-right: 10px"
        v-model="filters.market"
      />&nbsp;
      客户分类:<el-tree-select
        clearable
        check-strictly
        v-model="filters.category"
        :data="treeData"
        :render-after-expand="false"
        style="width: 150px"
        :props="{
          children: 'children',
          label: 'name',
          value: 'id'
        }"
      />&nbsp;
      客户类型:<el-tree-select
        clearable
        check-strictly
        v-model="filters.customerType"
        :data="treeData"
        :render-after-expand="false"
        style="width: 150px"
        :props="{
          children: 'children',
          label: 'name',
          value: 'id'
        }"
      /><br/>
      所属销售:<el-tree-select
        clearable
        check-strictly
        v-model="filters.saleId"
        :data="treeData"
        :render-after-expand="false"
        style="width: 150px"
        :props="{
          children: 'children',
          label: 'name',
          value: 'id'
        }"
      />&nbsp;
      <el-button type="primary" @click="init">查询</el-button>
      <el-button type="primary" @click="reset">重置</el-button>
    </div>
    <!--按钮区-->
    <div class="too">
      <div class="box">
        <el-button type="primary" @click="save()">添加</el-button>
        <el-button type="primary" >导入</el-button>
        <el-button type="primary" >导出</el-button>
      </div>
    </div>
    <!--表格数据-->
    <el-table :data="tableData" style="width: 100%" stripe border>
      <el-table-column
        label="客户ID"
        prop="id"
        width="80"
        header-align="center"
        align="center"
      />
      <el-table-column
        prop="companyName"
        label="客户公司名称"
        width="150"
        header-align="center"
      />
      <el-table-column
        prop="name"
        label="联系人"
        width="68"
        header-align="center"
      />
      <el-table-column
        prop="mobile"
        label="联系方式"
        width="110"
        header-align="center"
        align="center"
      >
      </el-table-column>
      <el-table-column
      prop="market"
      label="市场"
      width="68"
      header-align="center">
      <template #default="{ row }">
        <span v-if="row.market == 1">内销</span>
        <span v-else-if="row.market == 2">外销</span>
      </template>
      </el-table-column>
      <el-table-column
        prop="customerType"
        label="客户类型"
        width="83"
        header-align="center"
        align="center"
      >
        <template #default="{ row }">
          <span v-if="row.customerType == 1">普通用户</span>
          <span v-else-if="row.customerType == 2">潜在用户</span>
          <span v-else>外销</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="email"
        label="邮箱"
        width="163"
        header-align="center"
      />
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="155"
        header-align="center"
      />
      <el-table-column
      prop="saleId"
      label="所属销售"
       width="110"
      header-align="center" />
      <el-table-column
        label="操作"
        header-align="center"
        align="center"
      >
        <template #default="{ row }">
          <el-button
            type="primary"
            :icon="Edit"
            circle
            title="编辑"
            @click="save(row)"
          />
          <el-button
            type="primary"
            :icon="List"
            circle
            title="下单记录"
          />
          <el-button
            type="primary"
            :icon="Checked"
            circle
            title="出货记录"
          />
          <el-button
            type="primary"
            :icon="Lock"
            circle
            title="密码重置"
          />
          <el-button
            type="primary"
            :icon="Clock"
            circle
            title="浏览记录"
          />
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <div style="height: 60px; line-height: 60px; background-color: #fff">
      <el-pagination
        background
        layout="sizes,total,prev, pager, next,jumper"
        :total="total"
        style="float: right; margin-right: 20px"
        @current-change="chpg"
        @size-change="chsize"
      />
    </div>
  </div>
</template>

<!--vue 组件选项定义-->
<script setup>
import { ref, onBeforeMount } from "vue";
import { Delete, Edit, Clock, List, Checked, Lock } from "@element-plus/icons-vue";
import { querycustomer } from "@/api/customer.js";
import { useRouter } from "vue-router";
//表格数据
const tableData = ref([]);

//获取路由实例
const router = useRouter();
//分页参数
const page = ref(1); //当前页数
const pageSize = ref(10); //每页记录数
const total = ref(0); //总记录数

//搜索 条件
const filters = ref({
  id: "",
  companyName: "",
  mobile: "",
  name: "",
  market: "",
  customerType: "",
  category: "",
  saleId: "",
  productName: "",
  country: "",
  email: ""
});
//部门树
const treeData = ref([]);

//重置
function reset() {
  filters.value = {
    id: "",
    companyName: "",
    mobile: "",
    name: "",
    market: "",
    customerType: "",
    category: "",
    saleId: "",
    productName: "",
    country: "",
    email: ""
  };
  init();
}

//新增
function save(data) {
  //跳转
  // router.push("/customer/add");
  router.push({ path: "/customer/add" });
  /* const routeUrl = router.resolve({
    path: "/customer/add"
  });
  window.open(routeUrl.href, "_top");*/
}

//方法
async function init() {
  let data = {
    page: page.value,
    pageSize: pageSize.value,
    name: filters.value.name,
    mobile: filters.value.mobile,
    id: filters.value.id,
    companyName: filters.value.companyName,
    market: filters.value.market,
    customerType: filters.value.customerType,
    category: filters.value.category,
    saleId: filters.value.saleId,
    productName: filters.value.productName,
    country: filters.value.country,
    email: filters.value.email
  };
  let res = await querycustomer(data);
  if (res.code == 200) {
    tableData.value = res.data.list;
    page.value = res.data.page;
    pageSize.value = res.data.pageSize;
    total.value = res.data.total;
  }
}

//改变当前页
function chpg(pg) {
  page.value = pg;
  init();
}
//改变每页记录数
function chsize(pgsize) {
  pageSize.value = pgsize;
  page.value = 1;
  init();
}
//生命周期钩子
onBeforeMount(() => {
  init();
});
</script>

<!--vue样式 scoped当前样式只适用于当前组件-->
<style scoped>
.toolbar {
  height: 180px;
  line-height: 60px;
  background-color: #fff;
  padding-left: 20px;
}
.too {
  height: 60px;
  line-height: 60px;
  padding-left: 20px;
}
.box{
  height: 60px;
  width: 210px;
  margin-left: auto;
}
</style>
